package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.excel.EasyExcel;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.listerner.DemoDataListener;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrdersettingService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author hql
 * @date 2019-12-05 12:16
 */
@RestController
@RequestMapping("/ordersetting")
public class OrdersettingController {
    @Reference
    private OrdersettingService ordersettingService;

    /**
     * 将Excel文件上传到数据库中
     * @param excelFile
     * @return
     */
    @RequestMapping("/upload")
    public Result upload(MultipartFile excelFile){
        try {
            //拿到list集合
            DemoDataListener demoDataListener = new DemoDataListener();
            //通过easyExcel读取导Excel表格中的数据
            EasyExcel.read(excelFile.getInputStream(), OrderSetting.class, demoDataListener).sheet().doRead();
            List<OrderSetting> orderSettingList = demoDataListener.getOrderSettingList();
            //调用ordersettingService插入或者修改表中的数据
              ordersettingService.addOrUpdate(orderSettingList);

        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.UPLOAD_Fail);
        }

        return new Result(true,MessageConstant.UPLOAD_SUCCESS);
    }

    /**
     * 查询所有的预约内容
     * @param date
     * @return
     */
    @RequestMapping("/findAllOrdersetting")
    public Result findAllOrdersetting(String date){
        List<Map> map =null;
        try {
            //拿到list集合，按照前台所需的json串
          map= ordersettingService.findAllOrdersetting(date);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }

        return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,map);
    }

    /**
     * 设置或者修改预约的人数
     * @param orderSetting
     * @return
     */
    @RequestMapping("/changeNumber")
    public Result changeNumber(@RequestBody OrderSetting orderSetting){

        try {
            //调用ordersettingService方法
             ordersettingService.changeNumber(orderSetting);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }

        return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
    }

}
